import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/models/case_list_entity.dart';
import 'package:seedfour/app/routes/app_routes.dart';
import 'package:seedfour/app/services/api_service.dart';
import 'package:seedfour/app/services/common_service.dart';

class CaseLogic extends GetxController with GetSingleTickerProviderStateMixin {
  final apiService = Get.find<ApiService>();
  final commonService = Get.find<CommonService>();
  final loadingTip = '加载中...';
  final noMoreTip = '没有更多了';

  var tabTitles = ['进行中', '待结案', '已结案'].obs;
  var isLoading = true.obs;
  var isSearching = false.obs;
  var isSearched = false.obs;
  var bottomTips = ''.obs;
  var caseList = <CaseInfo>[].obs;

  late TabController tabController;
  late ScrollController scrollController;
  late TextEditingController searchController;

  final pageSize = 20;
  final typeParam = ['apply', 'settle', 'complete'];
  int _page = 1;
  int _type = 0;

  @override
  void onInit() {
    super.onInit();
    tabController = TabController(length: tabTitles.length, vsync: this);
    tabController.addListener(() {
      if (tabController.index.toDouble() == tabController.animation?.value) {
        _type = tabController.index;

        reload();
      }
    });

    scrollController = ScrollController();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        bottomTips.value = loadingTip;
        _getCaseList(_page, _type);
      }
    });

    searchController = TextEditingController();

    reload();
  }

  void onClickSearch() {
    isSearching.value = true;
    isSearched.value = false;
  }

  void onCancelSearch() {
    isSearching.value = false;
    isSearched.value = false;
    searchController.clear();
  }

  void onSearch() {
    if (searchController.text.isNotEmpty) {
      _page = 1;
      caseList.clear();
      isLoading.value = true;
      _getCaseList(_page, _type);
    }
  }

  void onClickItem(int index) {
    Get.toNamed(RouteGet.caseDetail, arguments: caseList[index].id);
  }

  void reload() {
    _page = 1;
    caseList.clear();
    isLoading.value = true;
    isSearched.value = false;
    isSearching.value = false;
    _getCaseList(_page, _type);
  }

  void _getCaseList(int page, int type) {
    apiService
        .listCase(
          page,
          pageSize,
          commonService.getRole(),
          typeParam[type],
          caseName: isSearching.value ? searchController.text : '',
        )
        .then((result) => {
              isLoading.value = false,
              isSearched.value = true,
              _page++,
              if (result != null)
                {
                  caseList.addAll(type == 0
                      ? result.apply!
                      : type == 1
                          ? result.settle!
                          : result.complete!),
                  if (result.apply?.length != pageSize)
                    {bottomTips.value = noMoreTip}
                }
              else
                {bottomTips.value = noMoreTip}
            });
  }

  @override
  void onClose() {
    tabController.dispose();
    scrollController.dispose();
    super.onClose();
  }
}
